package com.yang.exception;

import com.yang.bean.common.BaseRespVo;
import com.yang.controller.admin.AdminGoodsController;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


import javax.validation.ConstraintDeclarationException;

/**
 * @ClassName ExceptionControllerAdvice
 * @Description TODO
 * @Author zhangdongdong
 * @Date 2022/9/8 20:36
 * @Version 1.0
 */
//@RestControllerAdvice(assignableTypes = AdminGoodsController.class)
@RestControllerAdvice
public class ExceptionControllerAdvice extends Throwable {

    @ExceptionHandler({MethodArgumentNotValidException.class, ConstraintDeclarationException.class, })
    public BaseRespVo validationException(Exception e) {
        if (e instanceof MethodArgumentNotValidException) {
            MethodArgumentNotValidException methodArgumentNotValidException = (MethodArgumentNotValidException) e;
            String message = methodArgumentNotValidException.getBindingResult().getAllErrors().get(0).getDefaultMessage();
            return BaseRespVo.invalidBeanData(message);
        }
        if (e instanceof ConstraintDeclarationException) {
            ConstraintDeclarationException constraintDeclarationException = (ConstraintDeclarationException) e;
            String message = constraintDeclarationException.getMessage();
            return BaseRespVo.invalidMethodParameter(message+"qqqqqqqqqqqq");
        }

        return BaseRespVo.error(e.getMessage());
    }

    @ExceptionHandler(WXException.class)
    public BaseRespVo doWXException(Exception e) {
        return BaseRespVo.error(e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public BaseRespVo doException(Exception e) {
        return BaseRespVo.error(e.getMessage());
    }

}
